//
//  StatusPara.swift
//  Hyphenate-Demo-Swift
//
//  Created by JennyShi on 2018/4/24.
//  Copyright © 2018年 杜洁鹏. All rights reserved.
//

import Foundation
//
//  CnEvent.swift
//  foodApp1
//
//  Created by Jenny Shi on 2018/1/5.
//  Copyright © 2018年 Apple Inc. All rights reserved.
//

import UIKit
import os.log

class StatusPara: NSObject, NSCoding {
    
    struct PropertyKey {
        static let hasFriend = "hasFriend"
        
        static let friendOwner = "friendOwner"
        static let loginOwner = "loginOwner"
        static let sourceViewControllerType = "sourceViewControllerType"
        static let dateTitle = "dateTitle"
        static let date = "date"
        static let calendarTypeNumber = "calendarTypeNumber"
        static let matchOrFree = "matchOrFree"
        static let languageNow = "languageNow"
    }
    
    //是否从聊天模块看朋友的calendar而来
    var hasFriend: Bool
    //从聊天模块看朋友calendar的朋友userID
    var friendOwner: String
    //login进入系统的本人的userID
    var loginOwner: String
    //1: SelectDate进来的自由创建；2：EventListViewController进来的，可以看邀请回复情况
    //3：Match&Free进来的有日期和起止时间限制的创建；4：EventListViewController进来的别人创建邀请我的，我回复去不去
    //5：notification进来的别人创建邀请我的，我回复去不去
    var sourceViewControllerType: Int
    //进入本模块之前选中的日期的字符串，格式为“2018-01-01”
    var dateTitle: String
    //进入本模块之前选中的日期
    var date: Date
    //calendar类型：0-public，1-personal
    var calendarTypeNumber: Int
    //match或free按钮点击后取后端数据标志：0-Match，1-Free
    var matchOrFree: Int
    //手机目前使用的语言
    var languageNow: String
    
    //参数相互制约：
    //hasFriend为true，则sourceViewControllerType只能是2、3，所以要先判断hasFriend，不能先判断sourceViewControllerType
    //hasFriend为true，则friendOwner必须非空。loginOwner必须非空。
    //日期是否可更改现在是硬性规定match进来后不能改的，因为改了以后time的前后约束就不对了，约束是绑定固定日期的
    //match和free进来后创建的event必须是public的
    //不能创建昨天及更早的event
    
    static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
    static let ArchiveURL = DocumentsDirectory.appendingPathComponent("StatusParaArr")
    
    //MARK: Initialization
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(hasFriend, forKey: PropertyKey.hasFriend)
        aCoder.encode(friendOwner, forKey: PropertyKey.friendOwner)
        aCoder.encode(loginOwner, forKey: PropertyKey.loginOwner)
        aCoder.encode(sourceViewControllerType, forKey: PropertyKey.sourceViewControllerType)
        aCoder.encode(dateTitle, forKey: PropertyKey.dateTitle)
        aCoder.encode(date, forKey: PropertyKey.date)
        aCoder.encode(calendarTypeNumber, forKey: PropertyKey.calendarTypeNumber)
        aCoder.encode(matchOrFree, forKey: PropertyKey.matchOrFree)
        aCoder.encode(languageNow, forKey: PropertyKey.languageNow)
    }
    
    required convenience init?(coder aDecoder: NSCoder) {
        // The title is required. If we cannot decode a name string, the initializer should fail.
        guard let loginOwner = aDecoder.decodeObject(forKey: PropertyKey.loginOwner) as? String else {
            os_log("Unable to decode the title for an object.", log: OSLog.default, type: .debug)
            return nil
            
        }
        
        // Because photo is an optional property of Meal, just use conditional cast.
        let hasFriend = aDecoder.decodeObject(forKey: PropertyKey.hasFriend) as? Bool
        let friendOwner = aDecoder.decodeObject(forKey: PropertyKey.friendOwner) as? String
        let sourceViewControllerType = aDecoder.decodeObject(forKey: PropertyKey.sourceViewControllerType) as? Int
        let dateTitle = aDecoder.decodeObject(forKey: PropertyKey.dateTitle) as? String
        let date = aDecoder.decodeObject(forKey: PropertyKey.date) as? Date
        let calendarTypeNumber = aDecoder.decodeObject(forKey: PropertyKey.calendarTypeNumber) as? Int
        let matchOrFree = aDecoder.decodeObject(forKey: PropertyKey.matchOrFree) as? Int
        let languageNow = aDecoder.decodeObject(forKey: PropertyKey.languageNow) as? String

        // Must call designated initializer.
        self.init(hasFriend : hasFriend!, friendOwner : friendOwner!, loginOwner : loginOwner, sourceViewControllerType : sourceViewControllerType!, dateTitle : dateTitle!, date : date!, calendarTypeNumber : calendarTypeNumber!, matchOrFree : matchOrFree!,languageNow: languageNow!)
        
    }
    
    init?(hasFriend : Bool, friendOwner : String, loginOwner : String, sourceViewControllerType : Int, dateTitle : String, date : Date, calendarTypeNumber : Int, matchOrFree : Int, languageNow : String) {
        
        // Initialization should fail if there is no name or if the rating is negative.
        if loginOwner.isEmpty  {
            return nil
        }
        
        // Initialize stored properties.
        self.hasFriend = hasFriend
        self.friendOwner = friendOwner
        self.loginOwner = loginOwner
        self.sourceViewControllerType = sourceViewControllerType
        self.dateTitle = dateTitle
        self.date = date
        self.calendarTypeNumber = calendarTypeNumber
        self.matchOrFree = matchOrFree
        self.languageNow = languageNow

    }
    
}
